/***************************************************
 ** @Desc : This file for 可变配置信息
 ** @Time : 2018.12.22 16:39 
 ** @Author : Joker
 ** @File : config
 ** @Last Modified by : Joker
 ** @Last Modified time: 2018.12.22 16:39
 ** @Software: GoLand
****************************************************/
package models

import (
	"sub_account/utils"
	"github.com/astaxie/beego/orm"
	"strconv"
)

type SysConfig struct {
	Id         int
	TransOut   string
	TransIn    string
	AppId      string
	PublicKey  string `orm:"type(text)"`
	PrivateKey string `orm:"type(text)"`
	Version    int
	EditTime   string
}

func (*SysConfig) TableEngine() string {
	return "INNODB"
}

func (*SysConfig) TableName() string {
	return ConfigTBName()
}

/*读取单个配置信息*/
func (*SysConfig) SelectOneConfig(id string) (conf SysConfig) {
	om := orm.NewOrm()
	_, err := om.QueryTable(ConfigTBName()).Filter("id", id).All(&conf)
	if err != nil {
		utils.LogError("SelectOneConfig failed to select config: ", err)
	}
	return conf
}

/*根据分账PID读取单个配置信息*/
func (*SysConfig) SelectOneConfigByTransOut(out string) (conf SysConfig) {
	om := orm.NewOrm()
	_, err := om.QueryTable(ConfigTBName()).Filter("trans_out", out).Limit(1).All(&conf)
	if err != nil {
		utils.LogError("SelectOneConfigByTransOut failed to select config: ", err)
	}
	return conf
}

/*读取全部配置信息,进行随机*/
func (*SysConfig) SelectAllConfig(trans_out string) (conf []SysConfig) {
	om := orm.NewOrm()
	_, err := om.QueryTable(ConfigTBName()).Filter("trans_out", trans_out).All(&conf)
	if err != nil {
		utils.LogError("SelectAllConfig failed to select config: ", err)
	}
	return conf
}

/*查询分页*/
func (*SysConfig) SelectConfigListPage(params map[string]interface{}, limit, offset int) (list []SysConfig) {
	om := orm.NewOrm()
	qt := om.QueryTable(ConfigTBName())
	for k, v := range params {
		if v != "" {
			qt = qt.Filter(k, v)
		}
	}
	_, err := qt.OrderBy("-trans_out").Limit(limit, offset).All(&list)
	if err != nil {
		utils.LogError("SelectConfigListPage failed to query paging for: ", err)
	}
	return list
}

func (*SysConfig) SelectConfigPageCount(params map[string]interface{}) string {
	om := orm.NewOrm()
	qt := om.QueryTable(ConfigTBName())
	for k, v := range params {
		if v != "" {
			qt = qt.Filter(k, v)
		}
	}
	cnt, err := qt.Count()
	if err != nil {
		utils.LogError("SelectConfigPageCount failed to count for:", err)
	}
	return strconv.FormatInt(cnt, 10)
}

/*修改配置信息*/
func (*SysConfig) UpdateSysConfig(conf SysConfig) int {
	om := orm.NewOrm()
	_, err := om.QueryTable(ConfigTBName()).Filter("id", conf.Id).Update(orm.Params{
		"version":     orm.ColValue(orm.ColAdd, 1),
		"trans_out":   conf.TransOut,
		"trans_in":    conf.TransIn,
		"app_id":      conf.AppId,
		"public_key":  conf.PublicKey,
		"private_key": conf.PrivateKey,
		"edit_time":   conf.EditTime,
	})
	if err != nil {
		utils.LogError("UpdateSysConfig failed to update for: ", err)
		return utils.FAILED_FLAG
	}
	return utils.SUCCESS_FLAG
}

/* *
 * @Author: Joker
 * @Description: 添加分账配置
 * @Date: 2018-12-28 14:38:17
 * @Param: SysConfig
 * @return: int: 判断成功的标识
 * @return: int64:	在表中哪一行插入
 **/
func (*SysConfig) InsertConfig(sr SysConfig) (int, int64) {
	om := orm.NewOrm()
	in, _ := om.QueryTable(ConfigTBName()).PrepareInsert()
	id, err := in.Insert(&sr)
	if err != nil {
		utils.LogError("InsertConfig failed to insert for: ", err)
		return utils.FAILED_FLAG, id
	}
	return utils.SUCCESS_FLAG, id
}

/*删除分账配置*/
func (*SysConfig) DeleteConfig(delId string) int {
	om := orm.NewOrm()
	_, err := om.QueryTable(ConfigTBName()).Filter("id", delId).Delete()
	if err != nil {
		utils.LogError("DeleteConfig delete error:", err)
		return utils.FAILED_FLAG
	} else {
		return utils.SUCCESS_FLAG
	}
}
